package com.hzt.agprosaleplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Address;
import com.hzt.agprosaleplatform.entity.User;
import com.hzt.agprosaleplatform.mapper.AddressMapper;
import com.hzt.agprosaleplatform.mapper.UserMapper;
import com.hzt.agprosaleplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    
    @Autowired
    AddressMapper addressMapper;

    @Override
    public boolean updateUserInfo(User user) {
        return userMapper.updateById(user) > 0;
    }
    
    @Override
    public IPage<User> getUsersPage(Page<User> page, String uid, String username, String role) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        
        // 根据条件构建查询
        if (uid != null && !uid.isEmpty()) {
            queryWrapper.eq("uid", uid);
        }
        if (username != null && !username.isEmpty()) {
            queryWrapper.like("username", username);
        }
        if (role != null && !role.isEmpty()) {
            queryWrapper.eq("role", role);
        }
        
        // 按照用户ID降序排列
        queryWrapper.orderByDesc("uid");
        
        return userMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }
    
    @Override
    public boolean updateUser(String uid, User user) {
        // 确保uid匹配
        user.setUid(uid);
        return userMapper.updateById(user) > 0;
    }
    
    @Override
    public boolean deleteUser(String uid) {
        return userMapper.deleteById(uid) > 0;
    }
    
    // 添加缺失的方法实现
    @Override
    public CustomResponse updateUserAvatar(String uid, MultipartFile file) throws IOException {
        // 这里应该实现更新用户头像的逻辑
        // 暂时返回一个默认响应
        return new CustomResponse(200, "头像更新成功", null);
    }
    
    @Override
    public CustomResponse updateUserPassword(String uid, String password) {
        // 这里应该实现更新用户密码的逻辑
        // 暂时返回一个默认响应
        return new CustomResponse(200, "密码更新成功", null);
    }
    
    @Override
    public CustomResponse addReceivedAddress(Address address) {
        // 实现添加用户地址的逻辑
        try {
            int result = addressMapper.insert(address);
            if (result > 0) {
                return new CustomResponse(200, "地址添加成功", address.getAid());
            } else {
                return new CustomResponse(500, "地址添加失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "地址添加失败：" + e.getMessage(), null);
        }
    }
    
    @Override
    public List<Address> UserAddressList(String uid) throws IOException {
        // 实现获取用户地址列表的逻辑
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        return addressMapper.selectList(queryWrapper);
    }
    
    @Override
    public CustomResponse updateReceivedAddress(Address address) throws IOException {
        // 实现更新用户地址的逻辑
        try {
            int result = addressMapper.updateById(address);
            if (result > 0) {
                return new CustomResponse(200, "地址更新成功", null);
            } else {
                return new CustomResponse(500, "地址更新失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "地址更新失败：" + e.getMessage(), null);
        }
    }
    
    @Override
    public CustomResponse deleteReceivedAddress(String aid) throws Exception {
        // 实现删除用户地址的逻辑
        try {
            int result = addressMapper.deleteById(aid);
            if (result > 0) {
                return new CustomResponse(200, "地址删除成功", null);
            } else {
                return new CustomResponse(500, "地址删除失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "地址删除失败：" + e.getMessage(), null);
        }
    }
}